package com.example.signsystem.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.example.signsystem.entity.dto.ClassScheduleDTO;
import com.example.signsystem.entity.parm.ClassScheduleParam;
import com.example.signsystem.entity.po.ClassPO;
import com.example.signsystem.service.IClassService;
import com.qcby.framework.common.pojo.Result;
import org.junit.jupiter.api.Test;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.ws.rs.POST;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;


@RestController
@RequestMapping("/sign-system/classes")
public class ClassController {

    @Resource
    IClassService classService;

    /**
     * 增加课程
     * @param classPO
     * @return
     */
    @GetMapping("/add-course")
    public Result<Boolean> notSignIn(ClassPO classPO){
        return Result.getSuccessResult(classService.getBaseMapper().insert(classPO)>0);
    }

    /**
     * 课程批量删除
     * @param ids
     * @return
     */
    @GetMapping("/deleteBatch")
    public Result<Boolean> deleteBatch(String ids){
        /**将字符串转成数组，ids为逗号分隔**/
        long[] longs = StrUtil.splitToLong(ids, ",");
        List<Long> idParam= Arrays.stream(longs).boxed().collect(Collectors.toList());
        return Result.getSuccessResult(classService.deleteBatch(idParam));
    }

    /**
     * 查看课表
     * @param classScheduleParam
     * @return
     */
    @GetMapping("/view-class-schedule")
    public Result<List<ClassPO>> viewClassSchedule(ClassScheduleParam classScheduleParam){
        return Result.getSuccessResult( classService.viewClassSchedule(classScheduleParam));
    }

    /**
     * 更新课表
     * @param classScheduleParam
     * @return
     */
    @PostMapping("/update-class-schedule")
    public Result<Boolean> updateById(@RequestBody @Valid ClassScheduleParam classScheduleParam){
        ClassScheduleDTO classScheduleDTO=BeanUtil.copyProperties(classScheduleParam,ClassScheduleDTO.class);
        return Result.getSuccessResult(classService.update(classScheduleDTO));
    }
}
